import { ref } from 'vue'
import { adapter, request } from '@/request'
import type { RequestWrapper } from '@/shared/generic'
import { setUserPwd } from '@/core/api/account.ts'
import { message } from 'ant-design-vue'
import is from '@/utils/is'

export interface UploadType {
  username: string
  pwd: string
  orderId: number
}

export default function setUserPwdFn() {
  const service = adapter<RequestWrapper<UploadType>>(request)
  const loading = ref(false)
  const response = ref<UploadType>()

  const fetch = (data: FormData) => {
    const { url, method } = setUserPwd()

    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      callback: (res) => {
        console.log('orderupFile 成功回调:', res)
        response.value = res.data.data
      },
      error: (err) => {
        console.error('orderupFile 错误回调:', err)
        if (is.error(err)) {
          message.error(err.message)
        }
      },
      done: () => {
        console.log('orderupFile 完成')
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
